/*
                    Listbox Class Implementation
*/
#include "Listbox.h"

Listbox::Listbox()
{
    bExpanded = false;
    bVisible = true;
    bEnabled = true;

    Image = NULL;

    iWidth = 0;
    iHeight = 0;

    iBackColour = 0xFFFFFF;
}

Listbox::~Listbox()
{
    for (int i = 0; i < Listitems.size(); i++)
    {
        delete Listitems.at(i);
    }

    Listitems.clear();

    SDL_FreeSurface(Image);
}

int Listbox::Initialise(Point NewPosition, int iNewSize, string strNewFontFilepath, int iNewWidth, int iNewBackColour, SDL_Color NewForeColour)
{
    strFontFilepath = strNewFontFilepath;

    iSize = iNewSize;

    Position = NewPosition;

    iWidth = iNewWidth;

    iBackColour = iNewBackColour;

    Listitems.clear();

    ForeColour = NewForeColour;
}

int Listbox::HandleMouseDown(int iMouseX, int iMouseY)
{
    if (Listitems.size() <= 0) { return 0; }

    string strTemp = Listitems.at(0)->getText();

    if (Listitems.at(0)->HandleMouseDown(iMouseX, iMouseY) == 1)
    {
         if (bExpanded == false)
         {
              bExpanded = true;
         }
         else
         {
             bExpanded = false;
         }
    }
    else
    {
        if (bExpanded == true)
        {
            for (int i = 1; i < Listitems.size(); i++)
            {
                if (Listitems.at(i)->HandleMouseDown(iMouseX, iMouseY) == 1)
                {
                     Listitems.at(0)->setText(Listitems.at(i)->getText());
                }
            }
        }

        bExpanded = false;
    }

    RefreshItemPositions();

    if (Listitems.at(0)->getText() != strTemp) { return 1; }
}

int Listbox::HandleMouseMotion(int iMouseXREL, int iMouseYREL)
{
    return 0;
}

int Listbox::SetListItems(vector<string> ListItemNames)
{
    Listitems.clear();

    for (int i = 0; i < ListItemNames.size(); i++)
    {
        Listitems.push_back(new Listitem());
        Listitems.back()->Initialise(strFontFilepath, iSize, ListItemNames.at(i), ForeColour);
        Listitems.back()->setName(ListItemNames.at(i));

        // Index 0 is the currently selected item so an extra item
        // needs to be added here for the first list item.
        if (i == 0)
        {
            Listitems.push_back(new Listitem());
            Listitems.back()->Initialise(strFontFilepath, iSize, ListItemNames.at(i), ForeColour);
            Listitems.back()->setName(ListItemNames.at(i));
        }
    }

    RefreshItemPositions();
}

int Listbox::RefreshItemPositions()
{
    Listitems.at(0)->setPosition(Position);

    int ic = Listitems.at(0)->getHeight(); // temp counter

    for (int i = 1; i < Listitems.size(); i++)
    {
        if (i > 1)
        {
           ic += Listitems.at(i - 1)->getHeight();
        }

        Listitems.at(i)->setPosition(Point(Position.x, Position.y + ic));
    }

    iHeight = Listitems.at(0)->getHeight();

    if (bExpanded == true)
    {
        for (int i = 1; i < Listitems.size(); i++)
        {
            iHeight += Listitems.at(i)->getHeight();
        }
    }
}

int Listbox::BlitToSurface(SDL_Surface *screen)
{

}

int Listbox::BlitToFrame(SDL_Surface *surface, Point FramePosition)
{
    FramePos = FramePosition;

    if (bVisible == true)
    {
        SDL_Rect dstRect;

        dstRect.x = (int)Position.x + (int)FramePosition.x;
        dstRect.y = (int)Position.y + (int)FramePosition.y;
        dstRect.w = iWidth;
        dstRect.h = iHeight;

        SDL_FillRect(surface, &dstRect, iBackColour);

        Listitems.at(0)->BlitToFrame(surface, FramePosition);

        if (bExpanded == true)
        {
            for (int i = 1; i < Listitems.size(); i++)
            {
                Listitems.at(i)->BlitToFrame(surface, FramePosition);
            }
        }
    }
}

void Listbox::setPosition(Point NewPosition)
{
    Position = NewPosition;

    RefreshItemPositions();
}

Point Listbox::getPosition()
{
    return Position;
}

string Listbox::getSelectedItem()
{
    return Listitems.at(0)->getText();
}

void Listbox::setSelectedItem(string strNewSelected)
{
    for (int i = 1; i < Listitems.size(); i++)
    {
        if (Listitems.at(i)->getText() == strNewSelected)
        {
            Listitems.at(0)->setText(strNewSelected);
        }
    }
}

void Listbox::setExpanded(bool bNewExpanded)
{
    bExpanded = bNewExpanded;
}

bool Listbox::getExpanded()
{
    return bExpanded;
}
